//
//  LoginViewController.m
//  PhotoSelector
//
//  Created by jerry on 2017/4/19.
//  Copyright © 2017年 jerry. All rights reserved.
//

#import "LoginViewController.h"
#import <AFNetworking.h>
#import <CommonCrypto/CommonDigest.h>
#import "EvaluateViewController.h"
#import "AppDelegate.h"


//#define url @"http://172.19.162.118:8080/app"
#define url @"http://127.0.0.1:8080/app"


@interface LoginViewController ()

@property (weak, nonatomic) IBOutlet UITextField *userName;
@property (weak, nonatomic) IBOutlet UITextField *passWord;
@property (weak, nonatomic) IBOutlet UILabel *testLabel;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)LoginButton:(UIButton *)sender {
    NSString *userId = [NSString stringWithFormat:@"%@",self.userName.text];
    NSString *password = [NSString stringWithFormat:@"%@",self.passWord.text];
    AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
    if ([userId isEqual:@""]||[password isEqual:@""]) {
        [self AlerShow:@"":@"用户名或密码不能为空"];
    } else {
        NSDictionary *idandpass = @{@"phone":userId,@"password":password};
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:idandpass options:NSJSONWritingPrettyPrinted error:nil];
        NSString *test = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        NSDictionary *paramDic=@{@"method":@"userLogin",@"param":test};
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html",@"text/json",@"text/javascript", @"text/plain", nil];
        [manager POST:url parameters:paramDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSString *status = [responseObject objectForKey:@"status"];
            if (status.integerValue == -1) {
                NSDictionary *data = [responseObject objectForKey:@"data"];
                UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                delegate.phone = [data objectForKey:@"phone"];
                delegate.name = [data objectForKey:@"name"];
                EvaluateViewController *receive1 = [storyboard instantiateViewControllerWithIdentifier:@"MainViewController"];
                [self.navigationController presentViewController:receive1 animated:YES completion:nil];
            }else{
                [self AlerShow:@"登录失败" :[responseObject objectForKey:@"message"]];
            }
            NSLog(@"post success:%@",responseObject);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            [self AlerShow:@"登录失败":@"请检测您的网络"];
        }];
    }
    
}

-(void)AlerShow:(NSString *) Title :(NSString *) Text{
    UIAlertController *alertContro = [UIAlertController alertControllerWithTitle:Title message:Text preferredStyle:UIAlertControllerStyleAlert];
    [alertContro addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]];
        [self presentViewController:alertContro animated:YES completion:nil];
}

-(IBAction)textFielDoneEditing:(id)sender{
    [sender resignFirstResponder];
}

- (NSString *)getMd5_32Bit_String:(NSString *)srcString{
    const char *cStr = [srcString UTF8String];
    unsigned char digest[CC_MD5_DIGEST_LENGTH];
    CC_MD5(cStr,strlen(cStr),digest);
    NSMutableString *result = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH *2];
    for(int i =0; i < CC_MD5_DIGEST_LENGTH; i++)
        [result appendFormat:@"%02x", digest[i]];
    
    return result;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (IBAction)writeDone:(UITextField *)sender {
    [sender resignFirstResponder];
}

-(IBAction)backgroundTap:(id)sender{
    [self.userName resignFirstResponder];
    [self.passWord resignFirstResponder];
}

@end
